/*:
 * @plugindesc 楓之舞功能 - 戰鬥開始音效
 * @author 老饕
 * @version 1.0.0
 * @date 2020.4.29
 * @help 
 * ============================================================================
 * 可以在敵群事件中添加註釋，來改變戰鬥開始的音效。
 * ============================================================================
 *
 * 敵群事件註釋：（可與YEP_BattleBgmControl一同使用，規則相同）
 *
 *   <START SE Name: filename>
 *   - Replace 'filename' with the filename of the SE you wish to play at the
 *   start of battle without the file extension. The filename is also case
 *   sensitive. For example, if you wish to play Battle3.m4a, replace
 *   'filename' with only 'Battle3' and no '.m4a' in the tag. This is the only
 *   required tag. Once this tag is detected, only the remainder of the event
 *   page will be scanned. Other pages in the troop will be ignored. 
 *
 *   <START SE Volume: x>
 *   - Replace 'x' with a number between 0 and 100. This determines the volume
 *   of the SE being played when the battle starts. This is an optional tag.
 *   If not used, it will default to a volume of 90.
 *
 *   <START SE Pitch: x>
 *   - Replace 'x' with a number between 0 and 100. This determines the pitch
 *   of the SE being played when the battle starts. This is an optional tag.
 *   If not used, it will default to a pitch of 0.
 *
 *   <START SE Pan: +x>
 *   <START SE Pan: -x>
 *   - Replace 'x' with a number between -100 and 100. This determines the pan
 *   of the SE being played when the battle starts. This is an optional tag.
 *   If not used, it will default to a pan of 0.
*/


var SWDA_BattleStartSE = SWDA_BattleStartSE || {};

SWDA_BattleStartSE.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
  if (!SWDA_BattleStartSE.DataManager_isDatabaseLoaded.call(this)) return false;
  if (!SWDA_BattleStartSE._loaded) {
    this.processBattleStartSeNotetags1($dataTroops);
    SWDA_BattleStartSE._loaded = true;
  }
  return true;
};

DataManager.processBattleStartSeNotetags1 = function(group) {
  var length = group.length;
  for (var t = 1; t < length; t++) {
    var troop = group[t];
    if (!troop) continue;
    troop.specificStartSe = {
      name: undefined,
      volume: 90,
      pitch: 100,
      pan: 0,
      pos: 0
    }
    var pageLength = troop.pages.length;
    for (var p = 0; p < pageLength; ++p) {
      var page = troop.pages[p];
      if (page) this.processBattleStartSeTroopData(troop, page);
    }
  }
};

DataManager.processBattleStartSeTroopData = function(troop, page) {
  var length = page.list.length;
  var checked = false;
  for (var i = 0; i < length; i++) {
    var pageItem = page.list[i];
    if (pageItem) {
      if (pageItem.code === 108 || pageItem.code === 408) {
        var line = pageItem.parameters[0];
        if (line) {
          if (line.match(/<START SE NAME:[ ](.*)>/i)) {
            troop.specificStartSe['name'] = String(RegExp.$1);
            checked = true;
          } else if (line.match(/<START SE VOLUME:[ ](\d+)>/i)) {
            troop.specificStartSe['volume'] = parseInt(RegExp.$1);
          } else if (line.match(/<START SE PITCH:[ ](\d+)>/i)) {
            troop.specificStartSe['pitch'] = parseInt(RegExp.$1);
          } else if (line.match(/<START SE PAN:[ ]([\+\-]\d+)>/i)) {
            troop.specificStartSe['pan'] = parseInt(RegExp.$1);
          } else if (line.match(/<START SE POSITION:[ ](\d+)>/i)) {
            troop.specificStartSe['pos'] = parseInt(RegExp.$1);
          }
        }
      }
    }
    if (checked) {
      break;
    }
  }
};

SWDA_BattleStartSE.SoundManager_PlayBattleStart = SoundManager.playBattleStart;
SoundManager.playBattleStart = function() {
  var troop = $dataTroops[$gameTroop._troopId];
  if (troop.specificStartSe.name === undefined) {
    SWDA_BattleStartSE.SoundManager_PlayBattleStart.call(this);
  } else {
    this.playSpecificBattleStartSe(troop.specificStartSe);
  }
};

SoundManager.playSpecificBattleStartSe = function(settings) {
  AudioManager.playSe(settings, settings.pos);
};